package sg.edu.nus.iss.vmcs.price;

import java.util.HashMap;

public class PricingContext {
	private int context = 0;

	private PriceCalculator calc = null;
	private HashMap<Integer, PriceCalculator> lsCalc = null;
	private PricingContext child = null;

	public PricingContext() {
		lsCalc = new HashMap<Integer, PriceCalculator>();
	}
	
	public PricingContext(PricingContext child){
		this.child = child;
	}

	public void addCalc(int context) {
		this.calc = getCalc(context);
		if (this.calc != null) {
			if (!lsCalc.containsKey(context)) {
				lsCalc.put(Integer.valueOf(context), this.calc);
			}
		}
	}
	
	public void removeCalc(int context){
		if (lsCalc.containsKey(context)){
			lsCalc.remove(context);
		}
	}

	/**
	 * Only set one Calculator. Remove existing calculator
	 * @param context
	 */
	public void setPriceCalculator(int context) {
		lsCalc.clear();
		addCalc(context);
	}

	public PriceCalculator getCalc(int context) {
		PriceCalculator calc = null;
		switch (context) {
		case PriceCalculator.CASH_PRICE_CALCULATOR:
			calc = new CoinPriceCalculator();
			break;
		case PriceCalculator.NETS_PRICE_CALCULATOR:
			calc = new NetsPriceCalculator();
			break;
		case PriceCalculator.XMAS_PRICE_CALCULATOR:
			calc = new ChristmasPriceCalculator();
			break;
		}
		return calc;
	}

	public int getPrice(int originalPrice) {
//		return calc.getPrice(originalPrice);
		int price = originalPrice;
		for(PriceCalculator myCalc: lsCalc.values()){
			price = myCalc.getPrice(price);
		}
		
		return price;
	}
	

}
